<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../plugins/elementui/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-button type="primary" class="butT" @click="handleCreate()">新建菜单</el-button>
            </div>
            <el-table
                    :data="dataList"
                    style="width: 100%;margin-bottom: 20px;"
                    row-key="id"
                    border
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column
                        prop="name"
                        label="菜单名称"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="linkUrl"
                        label="访问路径"
                        sortable
                        width="180">
                </el-table-column>
                <el-table-column
                        prop="priority"
                        label="菜单顺序">
                </el-table-column>
                <el-table-column prop="icon" label="菜单图标">
                    <template slot-scope="scope">
                        <i class="fa" :class="scope.row.icon"></i>
                    </template>
                </el-table-column>
                <el-table-column
                        prop="description"
                        label="菜单说明">
                </el-table-column>

                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleCreate(scope.row)">新增子菜单</el-button>
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="菜单层级" prop="level">
                                                <el-select v-model="formData.level" disabled>
                                                    <el-option label="导航菜单" :value="1"></el-option>
                                                    <el-option label="链接菜单" :value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>

                                        <el-col :span="12">
                                            <el-form-item label="菜单图标" prop="icon">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="父级菜单">
                                                <el-select v-model="formData.parentMenuId" disabled>
                                                    <el-option v-for="(value, name) in menuNames"
                                                               :key="value"
                                                               :label="name"
                                                               :value="value"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>

                                        <el-col :span="12">
                                            <el-form-item label="菜单顺序">
                                                <el-input-number v-model="formData.priority" :min="1" :max="20"
                                                                 label="菜单顺序">
                                                </el-input-number>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="链接地址" prop="linkUrl">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>

                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="菜单说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>

                            </el-tab-pane>

                            <el-tab-pane label="权限信息" name="second" v-if="formData.level==2">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>权限名称</th>
                                            <th>关键字</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in permissionList">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>

                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit" open="findAllPermission()">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                                         label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单名称" prop="name">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单层级" prop="level">
                                                <el-select v-model="formData.level" disabled>
                                                    <el-option label="层级菜单" :value="1"></el-option>
                                                    <el-option label="路径菜单" :value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>

                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="菜单图标" prop="icon">
                                                <el-input v-model="formData.icon"/>
                                            </el-form-item>
                                        </el-col>

                                        <el-col :span="12">
                                            <el-form-item label="父级菜单">
                                                <el-select v-model="formData.parentMenuId" disabled>
                                                    <el-option v-for="(value, name) in menuNames"
                                                               :key="value"
                                                               :label="name"
                                                               :value="value"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="链接地址" prop="linkUrl">
                                                <el-input v-model="formData.linkUrl"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="菜单顺序">
                                                <el-input-number v-model="formData.priority" :min="1" :max="20"
                                                                 label="菜单顺序">
                                                </el-input-number>
                                            </el-form-item>
                                        </el-col>

                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="菜单说明">
                                                <el-input v-model="formData.description" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>

                            <el-tab-pane label="权限信息" name="second" v-if="formData.level==2">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>权限名称</th>
                                            <th>关键字</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in permissionList">
                                            <td>
                                                <input :id="c.id" v-model="permissionIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>

                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<script>
    var vue = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            permissionIds: [],
            permissionList: [],
            activeName: "first",
            menuNames: {
                '跟菜单': 0
            },
            formData: {
                priority: 1
            },//表单数据
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]
            }
        },
        //声明周期方法，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
            this.findAllPermission();
        },
        methods: {
            // 重置表单
            resetForm() {
                this.formData = {priority: 1};
                this.permissionIds = [];
                this.activeName = "first";
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //查询所有的权限信息
            findAllPermission() {
                //定义请求路径
                let url = "/permission/all";
                //发送异步请求
                axios.get(url).then(response => {
                    if (response.data.flag) {
                        this.permissionList = response.data.data;
                    } else {
                        this.$message.error(response.data.message);
                    }
                });
            },
            findAll() {
                //定义请求路径
                let url = "/menu";
                //发送异步请求
                axios.get(url).then(response => {
                    if (response.data.flag) {
                        let menus = response.data.data;
                        for (let i = 0; i < menus.length; i++) {
                            let menu = menus[i];
                            this.menuNames[menu.name] = menu.id;
                        }
                    } else {
                        this.$message.error(response.data.message);
                    }
                });
            },
            //分页查询
            findPage() {
                //定义请求路径
                let url = "/menu/page";
                //定义请求参数
                let params = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                };
                //发送异步请求
                axios.get(url, {params: params}).then(response => {
                    if (response.data.flag) {
                        this.dataList = response.data.data.rows; //数据列表
                        this.pagination.total = response.data.data.total; //数据总条数
                    } else {
                        this.$message.error(response.data.message);
                    }
                });

                this.findAll();
            },
            // 弹出添加窗口
            handleCreate(row) {
                this.resetForm();
                this.dialogFormVisible = true;
                this.formData.parentMenuId = row ? row.id : 0;
                this.formData.level = this.formData.parentMenuId == 0 ? 1 : 2;
            },
            //添加
            handleAdd() {
                //表单提交之前校验表单数据是否符合规则
                this.$refs["dataAddForm"].validate((valid) => {
                    if (valid) { //数据校验通过
                        var url = "/menu?permissionIds=" + this.permissionIds;
                        axios.post(url, this.formData).then(response => {
                            //隐藏新增窗口
                            this.dialogFormVisible = false;
                            //解析返回数据
                            if (response.data.flag) { //新增成功
                                this.$message.success(response.data.message);
                            } else {
                                this.$message.error(response.data.message);
                            }
                        }).finally(() => {
                            this.findPage();//刷新页面
                        });
                    } else { //数据校验不通过
                        this.$message('表单数据校验失败!');
                        return false;
                    }
                });
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //查询数据详情
                this.resetForm();
                //发送异步请求
                axios.get("/menu/" + row.id).then(response => {
                    if (response.data.flag) {
                        this.formData = response.data.data;
                        this.dialogFormVisible4Edit = true;
                    } else {
                        this.$message.error(response.data.message);
                    }
                });

                //查询菜单管理权限信息
                axios.get("/menu/" + row.id + "/permissions").then(response => {
                    if (response.data.flag) {
                        this.permissionIds = response.data.data;
                    }
                });
            },
            //编辑
            handleEdit() {
                //提交数据之前进行表单校验
                this.$refs['dataEditForm'].validate(valid => {
                    if (valid) {
                        //表单校验通过 , 发送异步请求
                        var url = "/menu?permissionIds=" + this.permissionIds;
                        axios.put(url, this.formData).then(response => {
                            //隐藏编辑窗口
                            this.dialogFormVisible4Edit = false;
                            if (response.data.flag) {
                                this.$message.success(response.data.message);
                            } else {
                                this.$message.error(response.data.message);
                            }
                        }).finally(() => {
                            //刷新页面
                            this.findPage();
                        });

                    } else {
                        //表单校验不通过
                        this.$message.error("表单数据校验失败!");
                        return false;
                    }
                });
            },
            // 删除
            handleDelete(row) {
                this.$confirm("删除次记录将会连同子记录一同删除", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //用户点击确定 , 发送异步请求
                    var url = '/menu/' + row.id;
                    //发送异步请求
                    axios.delete(url).then(response => {
                        if (response.data.flag) {
                            this.$message.success(response.data.message);
                            this.findPage();
                        } else {
                            this.$message.error(response.data.message);
                        }
                    }).catch(error => {
                        this.showMessage(error);
                        console.log(error)
                    });
                })
            },
            //权限不足提示
            showMessage(r) {
                if (r == 'Error: Request failed with status code 403') {
                    //权限不足
                    this.$message.error('无访问权限');
                    return;
                } else {
                    this.$message.error('未知错误');
                    return;
                }
            }
        }
    })
</script>
</html>